﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL.crm;
using Gift.Model.crm;
using Gift.Model.crm.eNum;

namespace WebSite.crm.order
{
    public partial class selectGoodsList :  BaseCrm
    {
        readonly t_DALGoods theGoods = new t_DALGoods();
        readonly t_DALBrand theBrand = new t_DALBrand();
        readonly t_DALOrderGoods theOrderGoods = new t_DALOrderGoods();

        t_Goods oGoods;
        t_OrderGoods oOrderGoods;
        DataTable table;

       



        protected void Page_Load(object sender, EventArgs e)
        {

            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(eRule.InlineOrder);
                btnSaveRefresh.OnClientClick = Grid1.GetNoSelectionAlertInTopReference("没有选中项！");
                this.BindBrandList();
                this.BindGrid();
            }
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        void BindBrandList()
        {
            table = theBrand.GetList("isnull(isDeleted,0)=0").Tables[0];
            this.ddlBrandList.DataValueField = "brandId";
            this.ddlBrandList.DataTextField = "brandName";
            this.ddlBrandList.DataSource = table;
            this.ddlBrandList.DataBind();
            this.ddlBrandList.Items.Insert(0, new ExtAspNet.ListItem("请选择品牌", "-1"));
            this.ddlBrandList.SelectedValue = "-1";
        }

        string GetSqlWhere
        {
            get
            {
                StringBuilder strWhere = new StringBuilder();
                strWhere.Append(" 1= 1 ");
                if (this.ddlBrandList.SelectedItem.Value != "-1")
                {
                    strWhere.AppendFormat(" and brandId = {0}", this.ddlBrandList.SelectedItem.Value);
                }
                if (this.txtPriceFrom.Text.Trim() != "")
                {
                    strWhere.AppendFormat(" and goodsPrice>={0} ", this.txtPriceFrom.Text);
                }
                if (this.txtPriceTo.Text.Trim() != "")
                {
                    strWhere.AppendFormat(" and goodsPrice<={0} ", this.txtPriceTo.Text);
                }


                if (this.txtKeywords.Text.Trim() != "")
                {
                    strWhere.AppendFormat(" and (goodsName like '{0}' or goodsCode like '{0}')", this.txtKeywords.Text.Trim());
                }
                return strWhere.ToString();
            }
        }


        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table = theGoods.GetListByPage(GetSqlWhere, string.Format("{0} {1}", e.SortField, e.SortDirection), Grid1.PageIndex, Grid1.PageSize).Tables[0];
            Grid1.DataSource = table;
            Grid1.DataBind();
            ViewState["sort"] = e.SortField;
            ViewState["dir"] = e.SortDirection;
        }
        private void BindGrid()
        {   // 1.设置总项数
            Grid1.RecordCount = theGoods.GetRecordCount(GetSqlWhere.Replace("T.", ""));
            string strOrderBy = string.Format("{0} {1} "
                                               , ViewState["sort"] == null ? null : ViewState["sort"].ToString()
                                               , ViewState["dir"] == null ? null : ViewState["dir"].ToString());
            if (strOrderBy.Length < 13)
            {
                strOrderBy = "goodsId desc ";
            }
            DataTable table = theGoods.GetListByPage(GetSqlWhere, strOrderBy, Grid1.PageIndex, Grid1.PageSize).Tables[0];

            Grid1.DataSource = table;
            Grid1.DataBind();
            Grid1.SelectedRowIndexArray = new int[] { };

        }





        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            int selectedCount = this.Grid1.SelectedRowIndexArray.Length;
            StringBuilder sbProIds = new StringBuilder();
            for (int i = 0; i < selectedCount; i++)
            {
                int rowIndex = this.Grid1.SelectedRowIndexArray[i];

                oGoods = theGoods.GetModel(int.Parse(this.Grid1.DataKeys[rowIndex][0].ToString()));
                if(!theOrderGoods.Exists(oGoods.goodsId,Request.QueryString["code"]))
                {
                    oOrderGoods = new t_OrderGoods();
                    oOrderGoods.orderCode = Request.QueryString["code"];
                    oOrderGoods.goodsId = oGoods.goodsId;
                    oOrderGoods.goodsCount = 1;
                    oOrderGoods.costPrice = oGoods.goodsPrice;
                    theOrderGoods.Add(oOrderGoods);
                }
  
            }
            ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference());
        }
    }
}